const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    queqinList: [],
    length1: "",
    saveUrl: '',
    signWay: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this;
    var sList = wx.getStorageSync('sList') || [];
    console.log(sList)
    var lessonid = e.lessonid;
    var keyid = e.keyid;

    var way = e.way;
    if (way == 'word') {
      this.setData({
        saveUrl: 'savaNoSign.php',
        signWay: '口令'
      })
    } else if (way == 'ewm') {
      this.setData({
        saveUrl: 'ewm/saveNoSign.php',
        signWay: '二维码'
      })
    } else if (way == 'map') {
      this.setData({
        saveUrl: 'map/saveNoSign.php',
        signWay: '地图'
      })
    }
    console.log(that.data.saveUrl)
    console.log(that.data.signway)
    console.log('lessonid:' + lessonid)
    console.log('keyid:' + keyid)
    if (keyid == undefined) {
      wx.request({
        url: app.globalData.ServerUrl + 'noSignList.php',
        data: {
          lessonid: lessonid,
        },
        method: 'POST',
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          console.log(res.data)
          var allList = res.data;
          if (res.data == false) {} else {
            for (var i = 0; i < sList.length; i++) {
              for (var j = 0; j < res.data.length; j++) {
                if (sList[i] == res.data[j].number) {
                  res.data.splice(j, 1);
                }
              }
            }
            that.setData({
              queqinList: res.data,
              length1: res.data.length
            })
            /**保存缺勤名单 */
            for (var i = 0; i < res.data.length; i++) {
              console.log(res.data[i], )
              wx.request({
                url: app.globalData.ServerUrl + that.data.saveUrl,
                data: {
                  lessonid: e.lessonid,
                  openid: res.data[i].openid,
                  number: res.data[i].number,
                  name: res.data[i].name,
                  signway: that.data.signWay
                },
                method: 'POST',
                header: {
                  'Content-Type': 'application/x-www-form-urlencoded'
                },
                success: function (res) {
                  console.log(res.data)
                },
                fail: function (res) {},
                complete: function (res) {},
              })
            }
          }
        },
        fail: function (res) {},
        complete: function (res) {},
      })
    } else {
      wx.request({
        url: app.globalData.ServerUrl + 'showNoSign.php',
        data: {
          keyid: keyid
        },
        method: 'POST',
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          console.log(res.data)
          if (res.data == null) {
            wx.showToast({
              title: '没人缺勤耶( •̀ ω •́ )y',
              icon: 'none'
            })
            setTimeout(function () {
              wx.navigateBack({
                delta: 0,
              })
            }, 1500);
          } else {
            that.setData({
              queqinList: res.data,
              length1: res.data.length
            })
          }
        },
        fail: function (res) {},
        complete: function (res) {},
      })
    }
  },

  onReady: function () {},
  onShow: function () {},
  onHide: function () {},
  onUnload: function () {
    wx.removeStorageSync("sList")
  },
  onPullDownRefresh: function () {},
  onReachBottom: function () {},
  onShareAppMessage: function () {}
})